home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Harvest C 1.3 / Source Code / PPSymTable.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-15  |  7.5 KB  |  357 lines  |  [TEXT/ALFA]

  1. /*
  2.     Harvest C
  3.     Copyright 1992 Eric W. Sink.  All rights reserved.
  4.     
  5.     This file is part of Harvest C.
  6.     
  7.     Harvest C is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU Generic Public License as published by
  9.     the Free Software Foundation; either version 2, or (at your option)
  10.     any later version.
  11.     
  12.     Harvest C is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.     
  17.     You should have received a copy of the GNU General Public License
  18.     along with Harvest C; see the file COPYING.  If not, write to
  19.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.     
  21.     Harvest C is not in any way a product of the Free Software Foundation.
  22.     Harvest C is not GNU software.
  23.     Harvest C is not public domain.
  24.  
  25.     This file may have other copyrights which are applicable as well.
  26.  
  27. */
  28.  
  29. /*
  30.  * Harvest C
  31.  * 
  32.  * Copyright 1991 Eric W. Sink   All rights reserved.
  33.  * 
  34.  * This file implements operations on the preprocessor symbol table.
  35.  * 
  36.  */
  37.  
  38. #include <Memory.h>
  39. #include "structs.h"
  40. #include "PPSymTable.h"
  41. #include "AbsString.h"
  42. #include "DynArray.h"
  43.  
  44. struct preprocsym {
  45.     unsigned short                  SymbolFlags;
  46.     PPSYMVia_t                      next;
  47.  
  48.     unsigned short                  ArgCount;
  49.     SymListVia_t                    Args;
  50.  
  51.     EString_t                       PPValue;
  52.     unsigned long                   nameID;
  53. };
  54.  
  55. #define PPTABLESIZE 59
  56.  
  57. struct PPstablist {
  58.     PPSYMVia_t                      table[PPTABLESIZE];
  59.     short                           count;
  60. };
  61.  
  62. /* Global variables */
  63.  
  64. PPSymListVia_t                  Defines;    /* the list of all symbols
  65.                          * defined my the
  66.                          * preprocessor.  This
  67.                          * includes both simple
  68.                          * #defines and macros. */
  69.  
  70. StringPoolVia_t                 PreprocStrings;
  71. DynArrayVia_t                   PreprocArray;
  72.  
  73. /* Functions */
  74.  
  75. #ifdef OTHERHASH
  76. unsigned long
  77. PPTableHash(char *name)
  78. /* This is the hash function for the symbol tables. */
  79. {
  80.     unsigned long                   h = 0;
  81.     unsigned long                   g;
  82.  
  83.     for (; *name; ++name) {
  84.     h = (h << 4) + *name;
  85.     if (g = h & 0xf0000000)
  86.         h = (h ^ (g >> 24)) & 0x0fffffff;
  87.     }
  88.     return h % PPTABLESIZE;
  89. }
  90.  
  91. #endif
  92.  
  93. unsigned long
  94. PPTableHash(char *name)
  95. /* This is the hash function for the symbol tables. */
  96. {
  97.     unsigned long                   h = 0;
  98.     unsigned long                   g;
  99.  
  100.     for (; *name; ++name) {
  101.     h = h * 65599 + *name;
  102.     }
  103.     return h % PPTABLESIZE;
  104. }
  105.  
  106. PPSYM_t                        *
  107. PP_ID2Ptr(PPSYMVia_t obj)
  108. {
  109.     return GetObject(PreprocArray, obj);
  110. }
  111.  
  112. PPSYMVia_t
  113. RawPPSymbol(char *name)
  114. {
  115. #ifdef Undefined
  116.     register PPSYMVia_t             raw;
  117.     raw = Ealloc(sizeof(PPSYM_t));
  118.     Via(raw)->next = 0;
  119.     Via(raw)->Args = 0;
  120.     Via(raw)->SymbolFlags = 0;
  121.     Via(raw)->PPValue = 0;
  122.     Via(raw)->ArgCount = 0;
  123.     Via(raw)->nameID = PutString(PreprocStrings, name);
  124.     return raw;
  125. #endif
  126.     register PPSYMVia_t             raw;
  127.     PPSYM_t                        *object;
  128.     AbsStringID                     s;
  129.     raw = AddObject(PreprocArray);
  130.     s = PutString(PreprocStrings, name);
  131.     object = PP_ID2Ptr(raw);
  132.     object->nameID = s;
  133.     return raw;
  134. }
  135.  
  136. PPSymListVia_t
  137. RawPPTable(void)
  138. /* Create an empty, unused PP Symbol Table. */
  139. {
  140.     register PPSymListVia_t         raw;
  141.     raw = Ealloc(sizeof(PPSymList_t));
  142.     if (raw) {
  143.     #ifdef OLDMEM
  144.     HLock((Handle) raw);
  145.     #endif
  146.     memset((void *) Via(raw), 0, sizeof(PPSymList_t));
  147.     #ifdef OLDMEM
  148.     HUnlock((Handle) raw);
  149.     #endif
  150.     }
  151.     Via(raw)->count = 0;
  152.     return raw;
  153. }
  154.  
  155. PPSYMVia_t
  156. PPTableAdd(PPSymListVia_t table, char *name)
  157. {
  158.     /*
  159.      * All the table manipulation routines may be modified later to make the
  160.      * tables vastly more efficient.
  161.      */
  162.     register PPSYMVia_t             tmp;
  163.     register unsigned long          hk;
  164.     PPSYM_t                        *object;
  165.     tmp = RawPPSymbol(name);
  166.     hk = PPTableHash(name);
  167.     assert(table);
  168.     object = PP_ID2Ptr(tmp);
  169.     object->next = Via(table)->table[hk];
  170.     Via(table)->table[hk] = tmp;
  171.     Via(table)->count++;
  172.     return tmp;
  173. }
  174.  
  175. PPSYMVia_t
  176. PPTableSearch(PPSymListVia_t table, char *name)
  177. /*
  178.  * This routine searches a table for the given name, returning the symbol
  179.  * record for that name if it was found, otherwise NULL.
  180.  */
  181. {
  182.     register PPSYMVia_t             tmp;
  183.     unsigned long                   hashval;
  184.     hashval = PPTableHash(name);
  185.     if (!Via(table)->count) return 0;
  186.     tmp = Via(table)->table[hashval];
  187.     while (tmp) {
  188.     if (!AbsStringCmp(PreprocStrings, GetPPSymNameID(tmp), name)) {
  189.         return tmp;
  190.     } else {
  191.         tmp = GetPPSymNext(tmp);
  192.     }
  193.     }
  194.     return 0;
  195. }
  196.  
  197. PPSYMVia_t
  198. PPTableRemove(PPSymListVia_t table, PPSYMVia_t name)
  199. /* Removes a given symbol from the table. */
  200. {
  201.     if (name) {
  202.     KillString(PreprocStrings, GetPPSymNameID(name));
  203.     }
  204.     return 0;
  205. }
  206.  
  207. #ifdef Undefined
  208. void
  209. FreePP(PPSYMVia_t cur)
  210. {
  211.     if (cur) {
  212.     FreePP(Via(cur)->next);
  213.     FreeSymbolList(Via(cur)->Args, 1);
  214.     Efree(Via(cur)->PPValue);
  215.     Efree(cur);
  216.     }
  217. }
  218.  
  219. #endif
  220.  
  221. void
  222. FreePPSymbolList(PPSymListVia_t table)
  223. {
  224.     int                             ndx;
  225.     if (table) {
  226. #ifdef Undefined
  227.     ndx = 0;
  228.     while (ndx < PPTABLESIZE) {
  229.         FreePP(Via(table)->table[ndx++]);
  230.     }
  231. #endif
  232.     Efree(table);
  233.     }
  234.     KillStringPool(PreprocStrings);
  235.     KillDynArray(PreprocArray);
  236. }
  237.  
  238. void
  239. InitPPTable(void)
  240. {
  241.     Defines = RawPPTable();
  242.     PreprocStrings = RawStringPool(1000);
  243.     PreprocArray = RawDynArray(sizeof(PPSYM_t), 100);
  244. }
  245.  
  246. void
  247. SetPPSymFlags(PPSYMVia_t s, unsigned short f)
  248. {
  249.     PPSYM_t                        *obj;
  250.     obj = PP_ID2Ptr(s);
  251.     assert(obj);
  252.     obj->SymbolFlags = f;
  253. }
  254.  
  255. unsigned short
  256. GetPPSymFlags(PPSYMVia_t s)
  257. {
  258.     PPSYM_t                        *obj;
  259.     obj = PP_ID2Ptr(s);
  260.     assert(obj);
  261.     return obj->SymbolFlags;
  262. }
  263.  
  264. void
  265. SetPPSymValue(PPSYMVia_t s, EString_t v)
  266. {
  267.     PPSYM_t                        *obj;
  268.     EString_t                       val;
  269.     val = AllocString(v);
  270.     obj = PP_ID2Ptr(s);
  271.     assert(obj);
  272.     if (v) {
  273.     obj->PPValue = val;
  274.     } else {
  275.     obj->PPValue = 0;
  276.     }
  277. }
  278.  
  279. EString_t
  280. GetPPSymValue(PPSYMVia_t s)
  281. {
  282.     PPSYM_t                        *obj;
  283.     obj = PP_ID2Ptr(s);
  284.     assert(obj);
  285.     return obj->PPValue;
  286. }
  287.  
  288. void
  289. SetPPSymArgCount(PPSYMVia_t s, int NumArgs)
  290. {
  291.     PPSYM_t                        *obj;
  292.     obj = PP_ID2Ptr(s);
  293.     assert(obj);
  294.     obj->ArgCount = NumArgs;
  295. }
  296.  
  297. void
  298. SetPPSymArgs(PPSYMVia_t s, SymListVia_t ParmNames)
  299. {
  300.     PPSYM_t                        *obj;
  301.     obj = PP_ID2Ptr(s);
  302.     assert(obj);
  303.     obj->Args = ParmNames;
  304. }
  305.  
  306. int
  307. GetPPSymArgCount(PPSYMVia_t s)
  308. {
  309.     PPSYM_t                        *obj;
  310.     obj = PP_ID2Ptr(s);
  311.     assert(obj);
  312.     return obj->ArgCount;
  313. }
  314.  
  315. PPSYMVia_t
  316. GetPPSymNext(PPSYMVia_t s)
  317. {
  318.     PPSYM_t                        *obj;
  319.     obj = PP_ID2Ptr(s);
  320.     assert(obj);
  321.     return obj->next;
  322. }
  323.  
  324. unsigned long
  325. GetPPSymNameID(PPSYMVia_t s)
  326. {
  327.     PPSYM_t                        *obj;
  328.     obj = PP_ID2Ptr(s);
  329.     assert(obj);
  330.     return obj->nameID;
  331. }
  332.  
  333. void
  334. GetPPSymName(PPSYMVia_t s, char *nm)
  335. {
  336.     GetAbsString(PreprocStrings, GetPPSymNameID(s), nm);
  337. }
  338.  
  339. int
  340. PPSymSearchArgNum(PPSYMVia_t s, char *ArgName)
  341. {
  342.     PPSYM_t                        *obj;
  343.     obj = PP_ID2Ptr(s);
  344.     assert(obj);
  345.     assert(ArgName);
  346.     return TableSearchNum(obj->Args, ArgName);
  347. }
  348.  
  349. int
  350. GetPPSymValueLength(PPSYMVia_t s)
  351. {
  352.     PPSYM_t                        *obj;
  353.     obj = PP_ID2Ptr(s);
  354.     assert(obj);
  355.     return strlen(Via(obj->PPValue));
  356. }
  357.